package com.module.api.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.module.admin.prj.pojo.PrjClient;
import com.module.admin.prj.pojo.PrjInfo;
import com.module.admin.prj.pojo.PrjVersion;
import com.module.admin.prj.service.PrjClientService;
import com.module.admin.prj.service.PrjInfoService;
import com.module.admin.prj.service.PrjVersionService;
import com.module.admin.prj.utils.PrjClientUtil;
import com.module.admin.sys.pojo.SysUser;
import com.module.admin.sys.service.SysUserService;
import com.system.comm.enums.Boolean;
import com.system.comm.utils.FrameStringUtil;
import com.system.comm.utils.FrameTimeUtil;
import com.system.handle.model.ResponseCode;
import com.system.handle.model.ResponseFrame;

/**
 * service接口
 * @author yuejing
 * @date 2016年1月29日 下午9:31:59
 * @version V1.0.0
 */
@RestController
public class ApiPrjVersionController {

    private final Logger LOGGER = LoggerFactory.getLogger(ApiPrjVersionController.class);
    
	@Autowired
	private PrjVersionService prjVersionService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private PrjInfoService prjInfoService;
	@Autowired
	private PrjClientService prjClientService;

	/**
	 * 部署服务
	 */
	@RequestMapping(value = "/api/prjVersion/deploy")
	public ResponseFrame deploy(String appcode, String username, String version, String remark, String url,
			String clients, String logpath) {
		try {
			ResponseFrame frame = new ResponseFrame();
			SysUser user = sysUserService.getByUsername(username);
			if (user == null) {
				frame.setCode(-2);
				frame.setMessage(username + " - 不存在");
				return frame;
			}
			PrjInfo pi = prjInfoService.getCode(appcode);
			if (pi == null) {
				frame.setCode(-2);
				frame.setMessage(appcode + " - 不存在");
				return frame;
			}
			if (FrameStringUtil.isEmpty(clients)) {
				frame.setCode(-2);
				frame.setMessage(appcode + " - 不存在要部署的客户端信息");
				return frame;
			}
			List<String> clientList = FrameStringUtil.toArray(clients, ",");
			if (clientList.size() == 0) {
				frame.setCode(-2);
				frame.setMessage(appcode + " - 不存在要部署的客户端信息");
				return frame;
			}
			
			if ("auto".equals(version)) {
				version = pi.getPrjId() + "_" + FrameTimeUtil.parseString(FrameTimeUtil.getTime(), "yyyyMMddHHmmss");
			}
			PrjVersion prjVersion = new PrjVersion();
			prjVersion.setPrjId(pi.getPrjId());
			prjVersion.setUserId(user.getUserId());
			prjVersion.setVersion(version);
			prjVersion.setRemark(remark);
			prjVersion.setIsRelease(Boolean.TRUE.getCode());
			prjVersion.setPathUrl(url);
			frame = prjVersionService.saveOrUpdate(prjVersion);
			StringBuffer message = new StringBuffer();
			
			for (String clientId : clientList) {
				// 保存客户端
				PrjClient prjClient = new PrjClient();
				prjClient.setPrjId(pi.getPrjId());
				prjClient.setClientId(clientId);
				prjClient.setVersion(version);
				prjClient.setLogPath(logpath);
				prjClientService.saveOrUpdate(prjClient);
				
				// 部署到客户端
				ResponseFrame releaseFrame = PrjClientUtil.releaseSingle(pi.getPrjId(), version, clientId);
				if (ResponseCode.SUCC.getCode() != releaseFrame.getCode()) {
					message.append("部署异常: ").append(releaseFrame.getMessage()).append("。");
				}
			}
			frame.setSucc();
			frame.setMessage(message.toString());
			return frame;
		} catch (Exception e) {
			LOGGER.error("处理业务异常: " + e.getMessage(), e);
			return new ResponseFrame(ResponseCode.SERVER_ERROR);
		}
	}

}